package com.wyh.onlyoffice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyh.onlyoffice.dto.ChangeUrl;
import com.wyh.onlyoffice.model.HistoryDoc;
import com.wyh.onlyoffice.model.User;
import com.wyh.onlyoffice.service.HistoryService;
import com.wyh.onlyoffice.service.IndexService;
import com.wyh.onlyoffice.service.UserService;
import com.wyh.onlyoffice.vo.HistoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class IndexServiceImpl implements IndexService {

    private final HistoryService historyService;
    private final UserService userService;

    public IndexServiceImpl(HistoryService historyService, UserService userService) {
        this.historyService = historyService;
        this.userService = userService;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert() {
        User user = new User();
        user.setName("李四");
        // 插入用户数据 saveOrUpdate: 保存或更新 如果传入的user实体没有id就是心中 有id就是更新 新增成功后用户就有id了[注：需要在表设计时设置id为自增长 实体类上也要标注自增长]
        if (!userService.saveOrUpdate(user)) {
            log.error("新增{}失败",user);
        }else{
            log.info("新增{}成功",user);
        }
        // 插入历史文档数据
        HistoryDoc history = new HistoryDoc();
        history.setUserId(user.getId());
        history.setServerVersion("6.4.2");
        history.setCreated("2022-07-09 08:44:32");
        history.setDocKey("1657356301616");
        history.setVersion("2");
        if (!historyService.saveOrUpdate(history)) {
            log.error("新增{}失败",history);
        }else{
            log.info("新增{}成功",history);
        }
        return true;
    }

    @Override
    public List<HistoryVo> history() {
        log.info("获取历史文档信息");
        List<HistoryVo> result = new ArrayList<>();
        List<HistoryDoc> list = historyService.list();
        List<Long> userIds = list.stream().map(HistoryDoc::getUserId).collect(Collectors.toList());
        List<User> users = userService.listByIds(userIds);
        Map<Long, List<User>> collect = users.stream().collect(Collectors.groupingBy(User::getId));
        for (HistoryDoc item : list) {
            List<User> userList = collect.get(item.getUserId());
            HistoryVo history = new HistoryVo();
            history.setServerVersion(item.getServerVersion());
            history.setCreated(item.getCreated());
            history.setKey(item.getDocKey());
            history.setVersion(item.getVersion());
            history.setUser(userList.get(0));
            result.add(history);
        }
        return result;
    }

    @Override
    public ChangeUrl changeUrls(String version) {
        HistoryDoc historyDoc = historyService.getOne(new QueryWrapper<HistoryDoc>().eq("version", version));
        ChangeUrl result = new ChangeUrl();
        result.setUrl(historyDoc.getUrl());
        result.setVersion(historyDoc.getVersion());
        result.setKey(historyDoc.getDocKey());
        return result;
    }
}
